//
//  NewView.m
//  Hero
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "NewView.h"
#import "WYScrollView.h"
#import "YQNetWorkRequestManager.h"
#import "ConsultTableViewCell.h"
#import "ConsultModel.h"
@interface NewView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) WYScrollView *myScrollView;
@property (nonatomic, strong) NSMutableArray *urlImageArray;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UITableView *myTableView;
@end
@implementation NewView
static int count = 0;
- (void)dealloc
{
    [self.myTableView removeObserver:[self.myTableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.myTableView removeObserver:[self.myTableView defaultFooter] forKeyPath:@"contentSize"];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.urlImageArray = [NSMutableArray new];
        self.dataArray = [NSMutableArray new];
        [self reloadData];
        [self set_View];
        UITableView *oneTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.bounds.size.height)];
        oneTableView.delegate = self;
        oneTableView.dataSource = self;
        [oneTableView registerNib:[UINib nibWithNibName:@"ConsultTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
        [self addSubview:oneTableView];
        self.myTableView = oneTableView;
        __weak typeof(self)weakSelf = self;
        self.myTableView.separatorStyle = 0;
        [self.myTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
            [weakSelf set_View];
        }];
        
    }
    return self;
}
- (void)reloadData
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml"] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in dic[@"list"]) {
                [self.urlImageArray addObject:dict[@"image_url_big"]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                self.myScrollView = [[WYScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150) WithNetImages:self.urlImageArray.copy];
                self.myScrollView.AutoScrollDelay = 3;
                self.myTableView.tableHeaderView = self.myScrollView;
            });
        }
    }];
}
- (void)set_View
{
    
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/c12_list_%d.shtml",++count]] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dic  in dict[@"list"]) {
                ConsultModel *model = [ConsultModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myTableView reloadData];
                [[self.myTableView defaultFooter] endRefreshing];
            });
        }
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConsultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConsultModel *model = self.dataArray[indexPath.row];
    [self.myDelegate didSelectCellWithModel:model];
}
@end
